package com.sskj.mine.ui.activity;


import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.presenter.PwdResetActivityPresenter;
import com.sskj.common.base.App;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.MD5Util;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.router.provider.LogoutProvider;

import butterknife.BindView;
import butterknife.ButterKnife;

@Route(path = RConfig.MINE_PWD_RESET)//密码重设
public class PwdResetActivity extends BaseActivity<PwdResetActivityPresenter> {

    @BindView(R2.id.etPwd)
    EditText etPwd;
    @BindView(R2.id.ivTogglePwd)
    ImageView ivTogglePwd;
    @BindView(R2.id.etPwdAgain)
    EditText etPwdAgain;
    @BindView(R2.id.ivTogglePwdAgain)
    ImageView ivTogglePwdAgain;
    @BindView(R2.id.btReset)
    Button btReset;
    @BindView(R2.id.tvResetToLogin)
    TextView tvResetToLogin;
    @Autowired(required = true)
    String phone;
    @Autowired(required = true)
    String code;
    boolean isSeeAgain;
    boolean isSee;

    @Override
    protected int getLayoutResId() {
        return R.layout.mine_activity_pwd_reset;
    }

    @Override
    public PwdResetActivityPresenter getPresenter() {
        return new PwdResetActivityPresenter();
    }

    @Override
    protected void initView() {
        ARouter.getInstance().inject(this);
        ClickUtil.click(btReset, () -> {
            if (!CheckUtil.isPayPwd(etPwd.getText().toString().trim()) || !CheckUtil.isPayPwd(etPwdAgain.getText().toString().trim())) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.minePwdResetActivity0));
                return;
            }
            if (!etPwd.getText().toString().trim().equals(etPwdAgain.getText().toString().trim())) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.minePwdResetActivity1));
                return;
            }

            mPresenter.resetPwdWithEmail(etPwd.getText().toString(), etPwdAgain.getText().toString().trim(), Integer.valueOf(code), phone);
//            if (CheckUtil.isMobile(phone)) {
//                mPresenter.resetPwd(phone, etPwd.getText().toString(), code);
//            } else {
//                mPresenter.resetPwdWithEmail(etPwd.getText().toString(), etPwdAgain.getText().toString().trim(), code, phone);
//            }

        });

        ClickUtil.click(200, ivTogglePwdAgain, () -> {
            if (isSeeAgain) {
                isSeeAgain = false;
                etPwdAgain.setTransformationMethod(PasswordTransformationMethod.getInstance());
                ivTogglePwdAgain.setImageResource(R.mipmap.mine_icon_unsee);
            } else {
                isSeeAgain = true;
                etPwdAgain.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                ivTogglePwdAgain.setImageResource(R.mipmap.mine_icon_see);
            }
        });
        ClickUtil.click(200, ivTogglePwd, () -> {
            if (isSee) {
                isSee = false;
                etPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                ivTogglePwd.setImageResource(R.mipmap.mine_icon_unsee);
            } else {
                isSee = true;
                etPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                ivTogglePwd.setImageResource(R.mipmap.mine_icon_see);
            }
        });
        ClickUtil.click(tvResetToLogin, () -> {
            ARouter.getInstance().build(RConfig.MINE_LOGIN).navigation();
        });
    }

    public void resetSuccess() {
        finish();
//        LogoutProvider logoutProvider = ARouter.getInstance().navigation(LogoutProvider.class);
//        logoutProvider.logout();
        ARouter.getInstance().build(RConfig.MINE_LOGIN).navigation();
    }
}
